package com.tao.mydownloadlibrary.helper;

import com.tao.mydownloadlibrary.DownloadStatue;
import com.tao.mydownloadlibrary.info.DownloadInfo;
import com.tao.mydownloadlibrary.utils.MyGosn;
import com.tao.mydownloadlibrary.utils.WriteStreamAppend;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * reate by tao on{DATE}
 * 文件字符串写入
 */
public class FileTextWriterReader {

    public static void writerSter(String str, String path) {
        FileOutputStream outputStream = null;
        try {
            File file = new File(path);
            if (!file.exists()) {
                file.getParentFile().mkdirs();
                file.createNewFile();
            }
            outputStream = new FileOutputStream(file);
            outputStream.write(str.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void readTextLins(String path, ReadTexeLineCall call) {
        if (call == null)
            return;
        BufferedReader bufferedReader = null;
        FileInputStream in = null;
        InputStreamReader inputStreamReader = null;
        try {
            in = new FileInputStream(new File(path));
            inputStreamReader = new InputStreamReader(in);
            bufferedReader = new BufferedReader(inputStreamReader);
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {

                String[] split = line.split(":");
                if (split.length < 2) {
                    continue;
                }
                File file = new File(split[1]);
                if (!file.exists()) {
                    continue;
                }
                String string = WriteStreamAppend.readFileString(file);
                if (string == null || string.isEmpty())
                    continue;
                if (call.onTextLine(string))
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }

        }

    }

    public interface ReadTexeLineCall {
        boolean onTextLine(String line);
    }
}
